﻿#if LOCK_STEP
using FLOAT = SoftFloat.sfloat;
using Time = NonLockStep.NTime;
#elif SERVICE
using FLOAT = System.Single;
using Time = Net.Entities.Time;
#else
using FLOAT = System.Single;
using Time = UnityEngine.Time;
#endif

namespace GameDesigner
{
    public class TimeStateMachine : IAnimationHandler
    {
        private FLOAT elapsedTime;
        private FLOAT normalizedTime;

        public TimeStateMachine()
        {
        }

        public void SetParams(params object[] args)
        {
        }

        public void OnInitializeBegin(IStateMachine stateMachine)
        {
            ApplyLayerMask();
            ApplyLayerWeight(false);
        }

        public void OnInitializeEnd(IStateMachine stateMachine)
        {
        }

        public void OnPlayAnimation(State state, StateAction stateAction)
        {
            stateAction.animTime = 0f;
            elapsedTime = 0f;
            normalizedTime = 0f;
        }

        public bool OnAnimationUpdate(State state, StateAction stateAction, StateMachineUpdateMode currMode)
        {
            if (currMode == StateMachineUpdateMode.Update)
            {
                if (stateAction.length <= 0f)
                    stateAction.length = 1f;
                elapsedTime += Time.deltaTime * state.animSpeed;
                normalizedTime = elapsedTime / stateAction.length;
                stateAction.animTime = normalizedTime / 1f * 100f;
            }
            return true;
        }

        public void OnAnimationEnd(State state, StateAction stateAction) { }

        public void ApplyLayerMask() { }

        public void ApplyLayerWeight(bool isCrossFade = true) { }

        public void OnStateEnter(State state) { }

        public void OnStateUpdate(State state, StateMachineUpdateMode currMode) { }

        public void OnStateExit(State state) { }
    }
}